package com.tma.timesheettracker.adapter;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.tma.timesheettracker.R;
import com.tma.timesheettracker.model.TrackingEntry;
import com.tma.timesheettracker.util.Util;

public class HistoryAdapter extends BaseAdapter {

	private Context context;
	private List<List<TrackingEntry>> entries;
	LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
			LinearLayout.LayoutParams.MATCH_PARENT,
			LinearLayout.LayoutParams.WRAP_CONTENT);

	public HistoryAdapter(Context context, List<List<TrackingEntry>> entries) {
		this.context = context;
		this.entries = entries;
	}

	public int getCount() {

		return entries.size();
	}

	public Object getItem(int arg0) {

		return null;
	}

	public long getItemId(int position) {

		return 0;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub

		layoutParams.setMargins(5, 5, 5, 3);
		LayoutInflater inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		convertView = inflater.inflate(R.layout.history_row, null);
		TextView lblHistoryDay = (TextView) convertView
				.findViewById(R.id.lblHistoryDate);
		LinearLayout lnhistory = (LinearLayout) convertView
				.findViewById(R.id.lnTimesheetHistory);

		lblHistoryDay.setText(Util.getDateFormat(entries.get(position).get(0)
				.getDate()));

		int durationOfday = 0;
		for (int i = 0; i < entries.get(position).size(); i++) {
			durationOfday += entries.get(position).get(i).getDuration();
			for (int j = 0; j < entries.get(position).get(i).getDuration(); j++) {
				TextView view = new TextView(context);
				view.setWidth(50);
				view.setHeight(10);
				view.setBackgroundColor(context.getResources().getColor(
						R.color.green));
				view.setLayoutParams(layoutParams);
				lnhistory.addView(view);
			}
		}
		for (int i = 0; i < Util.WORKING_HOURS - durationOfday; i++) {
			TextView view = new TextView(context);
			view.setWidth(50);
			view.setHeight(10);
			view.setBackgroundColor(context.getResources()
					.getColor(R.color.red));
			view.setLayoutParams(layoutParams);
			lnhistory.addView(view);
		}

		return convertView;
	}

}
